package com.huilai.platform.country_platform.controller;

import com.huilai.platform.country_platform.core.Result;
import com.huilai.platform.country_platform.model.User;
import com.huilai.platform.country_platform.service.ChatService;
import com.huilai.platform.country_platform.service.LoginService;
import com.huilai.platform.country_platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 作者：洪霓
 * 日期：2023/2/13
 * 注释：
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private ChatService chatService;

    @Autowired
    private UserService userService;

    @RequestMapping("/loginUI")
    public String loginUI() {
        return "login/index-login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(Model model, User user, HttpServletResponse response, HttpSession session) {
        Result result = loginService.login(user, response, session);
        return result;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        loginService.logout(request, response, session);
        //SpringMVC重定向

        return "redirect:/";
    }

    @RequestMapping("/registerUI")
    public String registerUI() {
        return "login/index-register";
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(Model model, User user) {
        return loginService.register(user);
    }
}
